/**
 * 
 */
package dt.inlee.system.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import dt.inlee.common.constant.CommonConstant;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.common.vo.PageBean;
import dt.inlee.domain.LogInfo;
import dt.inlee.domain.SysOrganization;
import dt.inlee.system.service.LogInfoService;
import dt.inlee.system.service.SysUserService;
import dt.inlee.system.vo.DictionaryBean;
import dt.inlee.system.vo.LogInfoBean;
import dt.inlee.system.vo.SysUserLoginBean;

/**
 * @功能说明：日志控制层
 * @作者： xgyin
 * @创建日期： 2011-3-22
 */
@Scope("prototype")
@Results( { @Result(name = "success", type = "json") })
@Controller("logInfoAction")
public class LogInfoAction extends ActionSupport {
	private static final long serialVersionUID = -8583349179831880315L;
	private List<DictionaryBean> logOrgs = new ArrayList<DictionaryBean>();
	@Autowired
	private LogInfoService logInfoService;
	@Autowired
	private SysUserService sysUserService;
	/** 日志vo */
	private LogInfoBean logInfoBean = new LogInfoBean();

	/** 日志vo集合 */
	private List<LogInfoBean> logInfoBeans = new ArrayList<LogInfoBean>();
	/** 日志Sumvo集合 */
	private List<LogInfoBean> logInfoSumBeans = new ArrayList<LogInfoBean>();
	
	private JSONObject jsonObject = new JSONObject();
	
	public JSONObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}

	private PageBean pageBean = new PageBean();

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public LogInfoBean getLogInfoBean() {
		return logInfoBean;
	}

	public void setLogInfoBean(LogInfoBean logInfoBean) {
		this.logInfoBean = logInfoBean;
	}

	public List<LogInfoBean> getLogInfoBeans() {
		return logInfoBeans;
	}

	public void setLogInfoBeans(List<LogInfoBean> logInfoBeans) {
		this.logInfoBeans = logInfoBeans;
	}

	public List<LogInfoBean> getLogInfoSumBeans() {
		return logInfoSumBeans;
	}

	public void setLogInfoSumBeans(List<LogInfoBean> logInfoSumBeans) {
		this.logInfoSumBeans = logInfoSumBeans;
	}

	@Action(value = "findAllLogInfos", results = { @Result(name = "success", type = "json", params = {
			"root", "jsonObject" }) })
	@SkipValidation()
	public String findAllLogInfos() throws FrameworkException, Exception {
		//jqGrid查询转码-utf-8
		BaseUtil.decodeObject(this.logInfoBean);
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		this.pageBean.init(this.logInfoService.getLogInfoTotalRows(this.logInfoBean));
		// 每页的行数
		int rows = Integer.parseInt((String) ServletActionContext
				.getRequest().getParameter("rows"));
		// 当前页码数
		int pageIndex = Integer.parseInt((String) ServletActionContext
				.getRequest().getParameter("page"));
		List<LogInfo> logInfos = this.logInfoService.findAllLogInfos(this.pageBean.getOrder(),"dtime", this.logInfoBean, pageIndex, rows);
		for (LogInfo logInfo : logInfos) {
			LogInfoBean bean = new LogInfoBean();
			bean.setBelongsCompany(logInfo.getBelongsCompany());
			bean.setDataid(logInfo.getDataid());
			bean.setDtime(logInfo.getDtime().toString());
			bean.setId(logInfo.getId());
			bean.setIp(logInfo.getIp());
			bean.setObjectname(logInfo.getObjectname());
			bean.setOperatorOrg(logInfo.getOperatorOrg());
			bean.setOperatorUser(logInfo.getOperatorUser());
			bean.setOptype(logInfo.getOptype());
			bean.setRemark(logInfo.getRemark());
			bean.setState(logInfo.getState());
			this.logInfoBeans.add(bean);
			
		}
		// ui需要实现数据的总记录数
		int count = pageBean.getTotalRecords();
		jsonMap.put("rows", logInfoBeans);
		jsonMap.put("total", count);
		jsonObject = JSONObject.fromObject(jsonMap);
		return SUCCESS;
	}

	@Action(value = "deleteLogInfos", results = { @Result(name = "success", type = "chain", location = "findAllLogInfos") })
	public String deleteLogInfos() throws FrameworkException {
		this.logInfoService.deleteLogInfoByIds(this.logInfoBean.getDeleteIDS().split(","));
		return SUCCESS;

	}

	@Action(value = "logInfoReportCount", results = { @Result(type = "json") })
	@SkipValidation()
	public String logInfoReportCount() throws FrameworkException {
		SysUserLoginBean loginUser = (SysUserLoginBean) ActionContext.getContext().getSession().get(
				CommonConstant.SESSION_USER);
		String orgName = loginUser.getOrganizationName().trim();
		String reportType = "0";//0:分公司；1:重庆电信，重庆移动，重庆联通；2:共建共享办公室,通信管理局;
		if (orgName.indexOf("分公司") > -1)
			reportType = "0";
		else if (orgName.equalsIgnoreCase("重庆电信") || orgName.equalsIgnoreCase("重庆移动")
				|| orgName.equalsIgnoreCase("重庆联通")) {
			reportType = "1";
			orgName = BaseUtil.isEmpty(this.logInfoBean) ? null
					: "分公司".equals(this.logInfoBean.getBelongsCompany()) ? orgName + "-"
							+ this.logInfoBean.getBelongsCompany() : this.logInfoBean.getBelongsCompany();
		} else if (orgName.equalsIgnoreCase("共建共享办公室") || orgName.equalsIgnoreCase("通信管理局")) {
			reportType = "2";
			orgName = BaseUtil.isEmpty(this.logInfoBean) ? null : this.logInfoBean.getBelongsCompany();
		}
		Date beginTime = BaseUtil.isEmpty(this.logInfoBean) ? null : this.logInfoBean.getBeginTime();
		Date endTime = BaseUtil.isEmpty(this.logInfoBean) ? null : this.logInfoBean.getEndTime();
		this.logInfoBeans = this.logInfoService.logInfoReportCount(reportType, orgName, beginTime, endTime);
		return SUCCESS;

	}

	@Action(value = "logInfoSumReportCount", results = { @Result(type = "json") })
	@SkipValidation()
	public String logInfoSumReportCount() throws FrameworkException {
		SysUserLoginBean loginUser = (SysUserLoginBean) ActionContext.getContext().getSession().get(
				CommonConstant.SESSION_USER);
		String orgName = loginUser.getOrganizationName().trim();
		String reportType = "0";//0:分公司；1:重庆电信，重庆移动，重庆联通；2:共建共享办公室,通信管理局;
		if (orgName.indexOf("分公司") > -1)
			reportType = "0";
		else if (orgName.equalsIgnoreCase("重庆电信") || orgName.equalsIgnoreCase("重庆移动")
				|| orgName.equalsIgnoreCase("重庆联通"))
			reportType = "1";
		else if (orgName.equalsIgnoreCase("共建共享办公室") || orgName.equalsIgnoreCase("通信管理局"))
			reportType = "2";
		Date beginTime = BaseUtil.isEmpty(this.logInfoBean) ? null : this.logInfoBean.getBeginTime();
		Date endTime = BaseUtil.isEmpty(this.logInfoBean) ? null : this.logInfoBean.getEndTime();
		this.logInfoSumBeans = this.logInfoService.logInfoSumReportCount(reportType, orgName, beginTime, endTime);
		return SUCCESS;

	}

	/**
	 * 所有的日志组织机构
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "findAllLogOrgs", results = { @Result(type = "json") })
	@SkipValidation()
	public String findAllLogOrgs() throws FrameworkException {
		List<SysOrganization> orgs = this.logInfoService.findAllLogOrgs();
		for (SysOrganization org : orgs) {
			DictionaryBean bean = new DictionaryBean();
			bean.setName(org.getName());
			bean.setValue(org.getName());
			this.logOrgs.add(bean);
		}
		return SUCCESS;

	}

	public List<DictionaryBean> getLogOrgs() {
		return logOrgs;
	}

	public void setLogOrgs(List<DictionaryBean> logOrgs) {
		this.logOrgs = logOrgs;
	}

}
